/**
 * Created by reeves on 2017/1/25.
 */
Template.postItem.helpers({
    domain: function () {
        var a = document.createElement('a');
        a.href = this.url;
        return a.hostname;
    },
    ownPost: function () {
        return this.userId === Meteor.userId();
    },
    commentsCount: function () {
        var result = Posts.findOne({_id: this._id});
        if (result) {
            return result.commentsCount;
        } else {
            return 0;
        }
    },
    newest: function () {
        var time = this.submitted;
        var dayBegin = new Date();
        dayBegin.setHours(0);
        dayBegin.setMinutes(0);
        dayBegin.setSeconds(0);
        dayBegin.setMilliseconds(0);

        if ((time - dayBegin) > 0 && (time - dayBegin) < 24 * 60 * 60 * 1000) {
            return true
        } else {
            return false;
        }
    },
    uploadTime: function () {
        var timeMinus = new Date() - this.submitted;

        if (timeMinus < 3 * 60 * 1000) {
            return "刚刚";
        } else if (timeMinus >= 3 * 60 * 1000 && timeMinus < 60 * 60 * 1000) {
            return parseInt(timeMinus / (60 * 1000)) + "分钟前";
        } else if (timeMinus >= 60 * 60 * 1000 && timeMinus < 24 * 60 * 60 * 1000) {
            return parseInt(timeMinus / (60 * 60 * 1000)) + "小时前";
        } else if (timeMinus >= 24 * 60 * 60 * 1000 && timeMinus < 30 * 24 * 60 * 60 * 1000) {
            return parseInt(timeMinus / (24 * 60 * 60 * 1000)) + "天前";
        } else if (timeMinus >= 30 * 24 * 60 * 60 * 1000 && timeMinus < 12 * 30 * 24 * 60 * 60 * 1000) {
            return parseInt(timeMinus / (30 * 24 * 60 * 60 * 1000)) + "月前";
        }else {
            return "挺久之前";
        }
    }
});